The goal of this step in the workflow is to compare S2 streamflow height to precipitation data in order to quality check the streamflow data.
Install/load necessary R packages
Set working directory if necessary (or create a file path to use throughout the RMD to call the data from Box)
Read in water year data from CSVs in the
intermediate_data directory
#read in necessary CSV files from prior RMDs
all_streamflow <- read_csv(here("intermediate_data", "all_streamflow.csv"))
air_temp_30_clean <- read_csv(here("intermediate_data", "air_temp_30_clean.csv"))
bogwell_clean <- read_csv(here("intermediate_data", "bogwell_clean.csv"))
mc_clean <- read_csv(here("intermediate_data", "mc_clean.csv")) %>%
rename(datetime = collected)
precip_15_clean <- read_csv(here("intermediate_data", "precip_15_clean.csv"))
precip_6h_clean <- read_csv(here("intermediate_data", "precip_6h_clean.csv"))
precip_12h_clean <- read_csv(here("intermediate_data", "precip_12h_clean.csv"))
precip_daily_clean <- read_csv(here("intermediate_data", "precip_daily_clean.csv"))
se_clean <- read_csv(here("intermediate_data", "se_clean.csv"))
#set constants
min_year = 2017
max_year = 2021
For interactive plots,
cumulative precipitation is plotted as grey bars,
streamflow is plotted as a blue line,
and streamflow manual checkpoints are plotted as black dots.
#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_6h_clean$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)
#plot
p_6h <- ggplot() +
#precipitation data
geom_col(data = precip_6h_clean,
aes(x = datetime,
y = precip_10_in,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Precipitation (in/10): ", precip_10_in)),
colour = I("grey"),
size = 0.25) +
#streamflow data
geom_line(data = all_streamflow,
aes(x = datetime,
y = stream_height_ft,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Stream Height (ft): ", round(stream_height_ft, digits = 3))),
color = "blue",
size = 0.4) +
#manual check points
geom_point(data = mc_clean,
aes(x = datetime,
y = stripchart_stage,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Stream Height Checkpoint (ft): ", stripchart_stage)),
size = 0.5) +
theme_classic() +
labs(x = "Time",
title = paste0("S2 Bog Streamflow and Cumulative \n Precipitation Every 6 Hours (", min_year, "-", max_year, ")"),
subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5,
size = 7)) +
scale_y_continuous(
#features of the first axis
name = "Stream Height (ft)",
#add a second axis and specify its features
##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Precipitation / 10 (in)")
)
#static plot
#p_6h
#save the plot in the figures folder
ggsave(filename = "streamflow_precip_6h_mc.png",
plot = p_6h,
path = "figures/",
width = 8,
height = 4,
units = c("in"),
dpi = 300)
#interactive plot
ggplotly(p_6h, tooltip = "text") %>%
layout(title = list(text = paste0(paste0("S2 Bog Streamflow and Cumulative \n Precipitation Every 6 Hours (", min_year, "-", max_year, ")"),
'<br>',
'<sup>',
paste0(" "),
'</sup>')),
font = list(size = 12)
)
For interactive plots,
cumulative precipitation is plotted as grey bars,
streamflow is plotted as a blue line,
and streamflow manual checkpoints are plotted as black dots.
#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_12h_clean$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)
#plot
p_12h <- ggplot() +
#precipitation data
geom_col(data = precip_12h_clean,
aes(x = datetime,
y = precip_10_in,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Precipitation (in/10): ", precip_10_in)),
colour = I("grey"),
size = 0.25) +
#streamflow data
geom_line(data = all_streamflow,
aes(x = datetime,
y = stream_height_ft,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Stream Height (ft): ", round(stream_height_ft, digits = 3))),
color = "blue",
size = 0.4) +
#manual check points
geom_point(data = mc_clean,
aes(x = datetime,
y = stripchart_stage,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Stream Height Checkpoint (ft): ", stripchart_stage)),
size = 0.5) +
theme_classic() +
labs(x = "Time",
title = paste0("S2 Bog Streamflow and Cumulative \n Precipitation Every 12 Hours (", min_year, "-", max_year, ")"),
subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5,
size = 7)) +
scale_y_continuous(
#features of the first axis
name = "Stream Height (ft)",
#add a second axis and specify its features
##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
###for example, max value of precipitation / max value of streamflow = 0.533...
sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Precipitation / 10 (in)")
)
#static plot
#p_12h
#save the plot in the figures folder
ggsave(filename = "streamflow_precip_12h_mc.png",
plot = p_12h,
path = "figures/",
width = 8,
height = 4,
units = c("in"),
dpi = 300)
#interactive plot
ggplotly(p_12h, tooltip = "text")
For interactive plots,
cumulative precipitation is plotted as grey bars,
streamflow is plotted as a blue line,
and streamflow manual checkpoints are plotted as black dots.
#format precipitation data for a smooth joining process
precip_daily_join <- precip_daily_clean %>%
pivot_wider(
names_from = "precip_type",
values_from = "precip_in"
) %>%
subset(year >= min_year & year <= max_year) %>%
rename(precip_10_in = precip_10) %>%
mutate(datetime = format(as.POSIXct(date, format = '%Y-%m-%d',
tz = "GMT"),
format = '%Y-%m-%d %H:%M:%S'),
datetime = as.POSIXct(datetime))
#save as CSV
write.csv(x = precip_daily_join,
file = file.path(here("intermediate_data", "precip_daily_join.csv")),
row.names = FALSE)
#join streamflow and daily precipitation data
streamflow_precip_daily <- full_join(x = all_streamflow,
y = precip_daily_join,
by = "date") %>%
select(-precip_in) %>%
subset(year >= min_year & year <= max_year)
#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_daily_join$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)
#plot
p_daily <- ggplot() +
#precipitation data
geom_col(data = precip_daily_join,
aes(x = datetime,
y = precip_10_in,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Precipitation (in/10): ", precip_10_in)),
colour = I("grey"),
size = 0.25) +
#streamflow data
geom_line(data = all_streamflow,
aes(x = datetime,
y = stream_height_ft,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Stream Height (ft): ", round(stream_height_ft, digits = 3))),
color = "blue",
size = 0.4) +
#manual check points
geom_point(data = mc_clean,
aes(x = datetime,
y = stripchart_stage,
group = 1,
#create hovering labels for interactive graph
text = paste0("DateTime: ", datetime, "\n",
"Stream Height Checkpoint (ft): ", stripchart_stage)),
size = 0.5) +
theme_classic() +
labs(x = "Time",
title = paste0("S2 Bog Streamflow and Cumulative \n Daily Precipitation (", min_year, "-", max_year, ")"),
subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5,
size = 7)) +
scale_y_continuous(
#features of the first axis
name = "Stream Height (ft)",
#add a second axis and specify its features
##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
###for example, max value of precipitation / max value of streamflow = 0.533...
sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Daily Precipitation / 10 (in)")
)
#static plot
#p_daily
#save the plot in the figures folder
ggsave(filename = "streamflow_precip_daily_mc.png",
plot = p_daily,
path = "figures/",
width = 8,
height = 4,
units = c("in"),
dpi = 300)
#interactive plot
ggplotly(p_daily, tooltip = "text")
Note that this file is being knit as index.html
into the precip_comparisons
repository in order to update the GitHub pages
website, where the plots can be viewed online.